package it.koutifaris.travianmap.db.impl;

import it.koutifaris.travianmap.db.Tile;
import it.koutifaris.travianmap.db.TileMapDAL;
import it.koutifaris.travianmap.model.Alliance;
import it.koutifaris.travianmap.model.MapSquare;
import it.koutifaris.travianmap.model.Player;
import it.koutifaris.travianmap.model.Tribe;
import it.koutifaris.travianmap.model.Village;
import android.graphics.Rect;

public class WorldMapDaoRandomImpl implements TileMapDAL {

	private static final double PROBABILITY = 0.1;
	private static final Rect BOUNDS = new Rect(-50, -50, +50, +50);

	private Player getPlayer() {
		int rnd = (int) (Math.random() * 4);
		Player player = new Player();
		Alliance alliance = new Alliance();
		alliance.setId(1);
		alliance.setName("Ally1");
		switch (rnd) {
		case 0:
			player.setName("Player1");
			player.setId(0);
			player.setTribe(Tribe.GAUL);
			player.setAlliance(alliance);
			break;
		case 1:
			player.setName("Player2");
			player.setId(1);
			player.setTribe(Tribe.ROMAN);
			player.setAlliance(alliance);
			break;
		case 2:
			player.setName("Player3");
			player.setId(2);
			player.setTribe(Tribe.TEUTON);
			break;
		case 3:
			player.setName("Player4");
			player.setId(3);
			player.setTribe(Tribe.NATARS);
			break;
		default:
		}
		return player;
	}

	@Override
	public Tile getTile(int x, int y) {
		TileImpl tile = new TileImpl();
		for (int i = x - Tile.TILE_RADIUS / 2; i <= x + Tile.TILE_RADIUS / 2; i++) {
			for (int j = y - Tile.TILE_RADIUS / 2; j <= y + Tile.TILE_RADIUS / 2; j++) {
				MapSquare square = new MapSquare();
				if (Math.random() < PROBABILITY) {
					square.setId(i + 400 + (j + 400) * 801);
					Village village = new Village();
					village.setId(square.getId());
					village.setName("V(" + i + ":" + j + ")");
					village.setOwner(getPlayer());
					square.setVillage(village);
				}
				square.setX(i);
				square.setY(j);
				tile.addSquare(square);
			}
		}
		return tile;
	}

	@Override
	public Rect getBounds() {
		return BOUNDS;
	}

}
